﻿var currentPage = 1; // 当前页
var pageSize = 50; // 每页显示多少条
var pageall = 0; // 总页数
var total = 0; // 总记录数

// 读取列表
$(function() {
    showList(currentPage);
});

// 条件查询资源列表
function showList(currentPage) {
    var build = $("#sCity2").val();                         // 城区ID
    var areaid = $("#sCity3").val();                        // 片区ID
    var name = $("#hEstateName").val();                     // 楼盘名称
    if (name == "楼盘名称、拼音均可" || $("#iEstateName").val() == "")
        name = "";
    var dateColumns = $("#sDateColumns").val();             // 日期类型
    var startday = $("#iStartDay").val();                   // 开始时间
    var endday = $("#iEndDay").val();                       // 结束时间
    var usage = $("#sUsage").val();                         // 用途
    var type = $("#sType").val();                           // 类型
    var isvalid = $("#sIsValid").val();                     // 状态 0有效 1待审核 2无效 3待匹配 -1全部
    var otherColumns = $("#sOtherColumns").val();           // 其它类型
    var otherColumnsValue = $("#iOtherColumnsValue").val(); // 其它类型值                       

    $("#divList").html("<img src=\"../images/loading.gif\" />"); // 清空列表 
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=estate_list_action&page=' + currentPage + '&pagesize=' + pageSize + "&build=" + build + "&areaid=" + areaid + "&name=" + name
                 + "&dateColumns=" + dateColumns + "&startday=" + startday + "&endday=" + endday + "&usage=" + usage + "&type=" + type + "&isvalid=" + isvalid
                  + "&otherColumns=" + otherColumns + "&otherColumnsValue=" + otherColumnsValue,
        timeout: '10000',
        error: function() {
            alert("系统繁忙,请稍候后试！");
        }, success: function(json) {
            listCallBack(json);
        }
    });

    // 审核权限判断
    var hCompetencesIsCheck = $("#hCompetencesIsCheck").val();      // 0不允许审核 1允许审核
    if (hCompetencesIsCheck == 1) {
        $("#Button1").removeAttr("disabled");
    } else {
        $("#Button1").attr("disabled", "true");
    }
}

/*
功能: 将返回的json数据进行排版
json: json数据
*/

function listCallBack(response) {    
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divList").html("<font color='red'>&nbsp;系统内暂无您查询的信息!</font>");
        return;
    }

    var page = json.page;

    currentPage = page.currentpage;
    pageall = parseInt(page.pageall);
    total = page.total;

    // 动态表头、动态列
    var fieldnames = json.field.fieldnames;
    var fieldvalues = json.field.fieldvalues;
    var fieldnameArray = new Array();
    var fieldvalueArray = new Array();

    fieldnameArray = fieldnames.split(",");
    fieldvalueArray = fieldvalues.split(",");

    var fieldnameHtml = ""; // 表头
    for (i = 0; i < fieldnameArray.length; i++) {
        fieldnameHtml += "<td height='25' bgcolor='d3eaef' class='STYLE6'>";
        fieldnameHtml += fieldnameArray[i];
        fieldnameHtml += "</td>";
    }
    var fieldslength = fieldvalueArray.length;      // 字段总数

    var arr = [];
    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("<input type=\"checkbox\" name=\"checkboxAll\" id=\"checkboxAll\" onclick=\"cbSelectAll()\" />");
    arr.push("</td>");
    arr.push(fieldnameHtml);
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("用途");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("状态");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("审核意见");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
    arr.push("匹配率");
    arr.push("</td>");
    
    // 权限判断
    var hCompetencesIsEdit = $("#hCompetencesIsEdit").val();                // 0不允许修改 1允许修改
    var hCompetencesIsOtherName = $("#hCompetencesIsOtherName").val();      // 0不允许申请别名 1允许申请别名
    if (hCompetencesIsEdit == 1 || hCompetencesIsOtherName == 1) {
        arr.push("<td height='25' bgcolor='d3eaef' class='STYLE10'>");
        arr.push("基本操作");
        arr.push("</td>");
    }
    arr.push("</tr>");

    var id_list = "";   // 保存查询出客源的ID 如 1211,1212,1213...
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        id_list += record[i].id;
        if (i < recordLength - 1)
            id_list += ",";

        if (i % 2 == 0)
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#FFFFFF' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");
        else
            arr.push("<tr id='tr_" + record[i].id + "' bgcolor='#EEEDED' style='cursor: pointer;' onclick=\"selectedTr('" + record[i].id + "')\" >");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push("<input type='checkbox' name='checkbox_" + record[i].id + "' id='" + record[i].id + "' onclick=\"cbSelect(this, " + record[i].id + ")\" />");
        arr.push("</td>");

        if (fieldslength > 0) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field1);
            arr.push("</td>");
        }
        if (fieldslength > 1) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push("<a href='#' onclick=\"openDetail('" + record[i].id + "')\">" + record[i].field2 + "</a>");
            arr.push("</td>");
        }
        if (fieldslength > 2) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field3);
            arr.push("</td>");
        }
        if (fieldslength > 3) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field4);
            arr.push("</td>");
        }
        if (fieldslength > 4) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field5);
            arr.push("</td>");
        }
        if (fieldslength > 5) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field6);
            arr.push("</td>");
        }
        if (fieldslength > 6) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field7);
            arr.push("</td>");
        }
        if (fieldslength > 7) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field8);
            arr.push("</td>");
        }
        if (fieldslength > 8) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field9);
            arr.push("</td>");
        }
        if (fieldslength > 9) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field10);
            arr.push("</td>");
        }
        if (fieldslength > 10) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field11);
            arr.push("</td>");
        }
        if (fieldslength > 11) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field12);
            arr.push("</td>");
        }
        if (fieldslength > 12) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field13);
            arr.push("</td>");
        }
        if (fieldslength > 13) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field14);
            arr.push("</td>");
        }
        if (fieldslength > 14) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field15);
            arr.push("</td>");
        }
        if (fieldslength > 15) {
            arr.push("<td height='20' class='STYLE6'>");
            arr.push(record[i].field16);
            arr.push("</td>");
        }
        
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(LexunJS.GetUsageNameById(record[i].usage));    
        arr.push("</td>");
        
        arr.push("<td height='20' class='STYLE6'>");

        //0有效 1待审核 2无效 3待匹配 4待整理 -1全部
        if (record[i].isvalid == 0)
            arr.push("(有效)");
        else if (record[i].isvalid == 1)
            arr.push("(待审核)");
        else if (record[i].isvalid == 2)
            arr.push("(无效)");
        else if (record[i].isvalid == 3)
            arr.push("(待匹配)");
        else if (record[i].isvalid == 4)
            arr.push("(待整理)");
        arr.push("</td>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].approvalviews);       
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].matchrate + "%");
        arr.push("</td>");

        arr.push("<td height='20'  class='STYLE6'>");
        if (record[i].isvalid == 3)
            arr.push("<a href='javascript:gotoMatch(" + record[i].id + ",\"" + record[i].field2 + "\")'>立即匹配</a> ｜ ");
            
        // 权限判断
        if (hCompetencesIsEdit == 1 && hCompetencesIsOtherName == 1) {            
            arr.push("<a href='javascript:add(" + record[i].id + ")'>申请别名</a> | ");
            arr.push("<a href='javascript:detail(" + record[i].id + ")'>修改</a> ");            
        } else if (hCompetencesIsEdit == 1 && hCompetencesIsOtherName == 0) {
            arr.push("<a href='javascript:detail(" + record[i].id + ")'>修改</a> ");
        } else if (hCompetencesIsEdit == 0 && hCompetencesIsOtherName == 1) {
            arr.push("<a href='javascript:add(" + record[i].id + ")'>申请别名</a>");
        }              
        arr.push("</td>");

        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divList").html(arr.join('')); // 显示列表
    $("#divPage").html(""); // 清空

    var pageHtml = LexunJS.showPage(currentPage, pageall, "showList"); // 分页
    $("#divPage").html(pageHtml);

    $("#hID_List").val(id_list);
}

// 响应键盘查询
function responseEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13)
        search();
}

// 条件查询
function search() {
    $("#divList").html(""); // 清空列表
    $("#divPage").html(""); // 清空分页
    
    showList(1);
}

// 单选
function cbSelect(obj, objvalue) {
    if (obj.checked) {
        LexunJS.addOrCutData(1, "hID", objvalue);
    }
    else {
        LexunJS.addOrCutData(0, "hID", objvalue);
    }
}

// 全选
function cbSelectAll() {
    document.getElementById("hID").value = "";
    var cbAll = document.getElementById("checkboxAll");
    var checkList = document.getElementsByTagName("input");

    if (cbAll.checked) {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = true;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(1, "hID", checkList[i].id);
            }
        }
    }
    else {
        for (var i = 0; i < checkList.length; i++) {
            if (checkList[i].type == "checkbox") {
                checkList[i].checked = false;
                if (parseInt(checkList[i].id) > 0) LexunJS.addOrCutData(0, "hID", checkList[i].id);
            }
        }
    }
}

// 级联加载列表
function loadCity(sCity1, sCity2) {
    var parentid = document.getElementById(sCity1).value;
    if (parentid <= 0)
        return false;

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=area_list&parentid=' + parentid,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            loadCity2CallBack(json, sCity2);
        }
    });
}

// 加载城市列表回调
function loadCity2CallBack(response, sCity2) {
    var json = eval("(" + response + ")");
    var record = json.record;

    $("#" + sCity2).html("");
    var obj = document.getElementById(sCity2);
    LexunJS.selectAddOption(obj, "全部", 0);

    if (record == null || record.length == 0)
        return;

    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        var sName = record[i].name;
        var sValue = record[i].id;

        LexunJS.selectAddOption(obj, sName, sValue);
    }
}

// 选中某行
function selectedTr(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");
}

// 查看详情
function openDetail(id) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    var id_list = $("#hID_List").val();
    var url = "../TM/EstateDetail.aspx?id=" + id + "&id_list=" + escape(id_list);

    self.parent.closeTab('楼盘详情');
    self.parent.addTab('楼盘详情', url, 'icon-nav');    
    //window.open("HousingDetail.aspx?id=" + id + "&id_list=" + escape(id_list), "target", LexunJS.openParams());
}

// 修改
function detail(id) {
    window.location.href = "EstateAdd.aspx?typeid=1&id=" + id;
}

// 1删除 2审核
function del(obj) {
    var IDList = document.getElementById("hID").value;
  
    if (obj == 1) {
       
       
        if (IDList == "") {
            alert("请先选择您要删除的行！");
            return false;
        }

        hiConfirm("您确定要删除该行，请谨慎操作！", "确认框", function(result) {
            if (result) {
                // ajax删除                   
                $.ajax({
                    type: 'POST',
                    url: '../Handler/HandlerIndex.ashx',
                    data: 'act=estate_del&type=1&id=' + IDList,
                    timeout: '10000',
                    error: function() {
                        alert("删除失败！");
                    }, success: function(outmsg) {
                        $("#hID").val("");
                        alert(outmsg);
                        showList(currentPage);
                    }
                });
            }
        });

        return false;
    }
    else {
        hiConfirm("您确定要审核?", "确认框", function(result) {
            if (result) {
                // ajax删除                   
                $.ajax({
                    type: 'POST',
                    url: '../Handler/HandlerIndex.ashx',
                    data: 'act=estate_del&type=2&id=' + IDList,
                    timeout: '10000',
                    error: function() {
                        alert("审核失败！");
                    }, success: function(outmsg) {
                        $("#hID").val("");
                        alert(outmsg);
                        showList(currentPage);
                    }
                });
            }
        });
    }
}

//修改楼盘别名
var eid = 0;
function add(id) {
    eid = id;
    openLayer("divList", "divLayer");
}

// 提交别名
function UpdateBuild() {
    //楼盘名称
    var name = document.getElementById("iEname").value;
    if (name.length = 0 || name.replace(/(^\s*)|(\s*$)/g, "") == "") {
        alert("别名不能为空！");
        document.getElementById("iEname").focus();
        return false;
    }

    var tname = 't_estate';
    var type = 1;
    var mName = document.getElementById("iEname").value;
    var cName = document.getElementById("iUserName").value;
    var cDept = document.getElementById("iUserDept").value;
    var cDId = document.getElementById("ideptid").value;
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerRoomNo.ashx',
        data: 'act=Update&id=' + eid + '&name=' + name + '&type=' + type + '&tname=' + tname + '&cName=' + cName + '&Cdept=' + cDept + '&cDId=' + cDId,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            Success(json);
        }
    });
}

function Success(response) {
    var json = eval("(" + response + ")");
    if (json > 0) {
        closeLayer();

    } else {
        alert("申请失败！");
    }
}


// typeid1审核全部栋阁房号 2删除全部栋阁房号
function checkAll(typeid) {
    var IDList = document.getElementById("hID").value; 
   
    if (IDList == "") {
        alert("请先选择您要操作的行！");
        return false;
    }

    if (!confirm("您确定要操作该楼盘下的全部栋阁房号，请谨慎操作！")) {
        return false;
    }

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=estate_delall&typeid=' + typeid + '&id=' + IDList,
        timeout: '10000',
        error: function() {
            alert("操作失败！");
        }, success: function(outmsg) {
            $("#hID").val("");
            alert(outmsg);
            showList(currentPage);
        }
    });
}

// 立即匹配
function gotoMatch(id, ename) {
    $("tr").removeClass("selected");
    $("#tr_" + id).addClass("selected");

    var id_list = $("#hID_List").val();
    var url = "../SM/SystemLoadHousingList2.aspx?eid=" + id + "&ename=" + escape(ename);

    self.parent.closeTab('楼盘盘源匹配');
    self.parent.addTab('楼盘盘源匹配', url, 'icon-nav');         
}